<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ios开关</title>
  <style>
 :root {
  --button-width: 500px;
  --button-height: 295px;
  --toggle-diameter: 255px;
  --button-toggle-offset: calc((var(--button-height) - var(--toggle-diameter)) / 2);
  --toggle-shadow-offset: 10px;
  --toggle-wider: 333px;
  --color-grey: #E9E9E9;
  --color-dark-grey: #39393D;
  --color-green: #30D158;
}

span {
  display: inline-block;
  width: var(--button-width);
  height: var(--button-height);
  background-color: var(--color-grey);
  border-radius: calc(var(--button-height) / 2);
  position: relative;
  transition: .3s all ease-in-out;
}

span::after {
  content: '';
  display: inline-block;
  width: var(--toggle-diameter);
  height: var(--toggle-diameter);
  background-color: #fff;
  border-radius: calc(var(--toggle-diameter) / 2);
  position: absolute;
  top: var(--button-toggle-offset);
  transform: translateX(var(--button-toggle-offset));
  box-shadow: var(--toggle-shadow-offset) 0 calc(var(--toggle-shadow-offset) * 4) rgba(0, 0, 0, .10);
  transition: .3s all ease-in-out;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}


/* 勾选状态下的样式 */
input[type="checkbox"]:checked + span {
  background-color: var(--color-green);
}
/* 控制勾选后圆形的位置 */
input[type="checkbox"]:checked + span::after {
  transform: translateX(calc(var(--button-width) - var(--toggle-diameter) - var(--button-toggle-offset)));
  box-shadow: calc(var(--toggle-shadow-offset) * -1) 0 calc(var(--toggle-shadow-offset) * 4) rgba(0, 0, 0, .10);
}

/* 隐藏原来的checkbox */
input[type="checkbox"] {
  display: none;
}


/* 控制点击未松开时候的样式 */
input[type="checkbox"]:active + span::after {
  width: var(--toggle-wider);
}


/* 控制点击未松开时候的样式 滑动到右侧特殊处理 */

input[type="checkbox"]:checked:active + span::after {
  transform: translateX(calc(var(--button-width) - var(--toggle-wider) - var(--button-toggle-offset)));
}

/* 判断系统是否使用了深色模式 */
@media(prefers-color-scheme: dark) { 
  body {
    background-color: #1C1C1E;
  }
  
  span {
    background-color: var(--color-dark-grey);
  }
}
  </style>
</head>

<body>
  <label for="toggle">
    <input type="checkbox" id="toggle">
    <span></span>
  </label>
</body>

</html>